package com.zonta.finance.serivce.impl;



import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.common.constant.CommonConstant;
import com.zonta.finance.common.exception.ParameterException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.UpdateEmailAndNameForm;
import com.zonta.finance.domain.po.Member;
import com.zonta.finance.mapper.MemberMapper;
import com.zonta.finance.serivce.IMemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-20
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    private final TokenService<AdminDTO> tokenService;

    /**
     *  会员表注册
     * @param tenantId
     */
    @Override
    public Long register(Long tenantId) {
        Member member = new Member();
        member.setTenantId(tenantId);
        member.setSysRoleIds("["+ CommonConstant.ROLE_MEMBER+"]");
        member.initDefault();
        if (!save(member)) {
            throw new ParameterException("会员注册异常");
        }
        return member.getId();


    }

    /**
     * 修改邮箱和手机号
     * @param form
     * @return
     */
    @Override
    public Boolean updateEmailAndName(UpdateEmailAndNameForm form) {
        LambdaUpdateWrapper<Member> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Member::getName,form.getName())
                .set(Member::getEmail,form.getEmail())
                .eq(Member::getTenantId,tokenService.getThreadLocalTenantId());
        return update(wrapper);
    }
}
